package lifeOfAFish.scenegraph.test;

import lifeOfAFish.test.JoglTemplate;
import lifeOfAFish.scenegraph.*;
import lifeOfAFish.scenegraph.geom.*;

import java.awt.event.KeyEvent;
import java.io.File;
import java.nio.IntBuffer;

import javax.media.opengl.*;
//import javax.media.opengl.GLAutoDrawable;

import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;



public class test_blending extends JoglTemplate {
	
	// the scene
	
	
	public static void main (String[] args){
		test_blending template = new test_blending();
		template.setVisible(true);
		
	}
	
	sgNode root = new sgNode();
	sgTransform trans1 = new sgTransform();
	sgTransform trans2 = new sgTransform();
	sgTransform trans3 = new sgTransform();
	sgTransform trans4 = new sgTransform();
	sgTransform trans5 = new sgTransform();
	sgGeomCube cube1 = new sgGeomCube(1.0f);
	sgGeomCube cube2 = new sgGeomCube(1.0f);
	sgGeomCube cube3 = new sgGeomCube(1.0f);
	sgGeomCube cube4 = new sgGeomCube(1.0f);
	sgGeomSphere sphere1 = new sgGeomSphere(0.75f);
	
	public void init(GLAutoDrawable drawable)
	{
		super.init(drawable);
		
		// get gl object
		GL gl = drawable.getGL();
		sgGlobals.gl = gl;
		
		// enable depth testing
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthRange(0.0, 1000.0);
		
		// enable lighting
//		gl.glEnable(GL.GL_COLOR_MATERIAL);
		gl.glEnable(GL.GL_LIGHTING);
		gl.glShadeModel(GL.GL_SMOOTH);
		
		
		// create the elements of the scene
		trans2.translate(0.0f, 0.0f, 5.0f);
		trans1.scale(2.0f, 2.0f, 2.0f);
		trans2.addChild(cube1);
		trans1.addChild(cube2);
		
		trans3.translate(5.0f, 0.0f, 0.0f);
		trans4.translate(-5.0f, 0.0f, 0.0f);
		trans3.addChild(cube3);
		trans4.addChild(cube4);
		
		trans5.translate(-0.25f, 0.1f, 3.0f);
		trans5.addChild(sphere1);
		
		root.addChild(trans1);
		root.addChild(trans2);
		root.addChild(trans3);
		root.addChild(trans4);
		root.addChild(trans5);
		
		// add two lightsources
		sgLight l1 = sgLightFactory.newLight();
		sgLight l2 = sgLightFactory.newLight();
		
		float[] p1 = {-2.0f, 3.0f, 4.0f, 1.0f};
		float[] p2 = {2.0f, -3.0f, 4.0f, 1.0f};
		
		l1.setPosition(p1);
		l2.setPosition(p2);
		
		sgMaterial mat1 = new sgMaterial();
		mat1.setAmbient(0.5f, 0.0f, 0.0f, 1.0f);
		mat1.setDiffuse(0.8f, 0.1f, 0.1f, 1.0f);
		mat1.setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
		cube2.setMaterial(mat1);
		
		
		sgMaterial mat2 = new sgMaterial();
		mat2.setAmbient(0.1f, 0.1f, 0.8f, 0.5f);
		mat2.setDiffuse(0.1f, 0.1f, 1.0f, 0.5f);
		mat2.setSpecular(1.0f, 1.0f, 1.0f, 0.5f);
		cube1.setMaterial(mat2);
		cube1.setOpaque(false);
		cube1.setName("blueCube");
		//cube1.setAlpha(0.5f);
		
		
		sgMaterial greenMat = new sgMaterial();
		greenMat.setAmbient(0.1f, 0.8f, 0.1f, 0.3f);
		greenMat.setDiffuse(0.1f, 0.8f, 0.1f, 0.3f);
		greenMat.setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
		sphere1.setMaterial(greenMat);
		sphere1.setOpaque(false);
		sphere1.setName("greenSphere");
		
		
		root.addChild(l1);
		root.addChild(l2);
	}
	
	/*
	 * overwritten version of display
	 */
	public void display(GLAutoDrawable drawable)
	{
		// get the gl object
		GL gl = drawable.getGL();
		// set the erasing color (black)
		gl.glClearColor(0f, 0f, 0f, 0f);
		
		// clear screen with the defined erasing color and depth buffer
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glPushMatrix();
		applyMouseTranslation(gl);
		applyMouseRotation(gl);
		
		root.traverse();
		sgGeomBlender.getInstance().render();
		
		gl.glPushMatrix();
		gl.glPopMatrix();

		// load an object using the mesh loader
		gl.glPopMatrix();
	}
	
	
	
}
